package com.eunion.web.cms.entity.spec;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.eunion.web.cms.entity.ScoreRank;

public final class ScoreRankSpec {

    public static Specification<ScoreRank> build(ScoreRank queryBean) {
        return new ScoreRankQuery(queryBean);
    }

    public static Specification<ScoreRank> build(ScoreRank queryBean,boolean accurate) {
        return new ScoreRankQuery(queryBean,accurate);
    }

    private static class ScoreRankQuery implements Specification<ScoreRank> {

        private ScoreRank queryBean;
        private boolean accurate = false;

        private ScoreRankQuery(ScoreRank queryBean) {
            this.queryBean = queryBean;
        }

        private ScoreRankQuery(ScoreRank queryBean,boolean accurate) {
            this.queryBean = queryBean;
            this.accurate = accurate;
        }


        @Override
        public Predicate toPredicate(Root<ScoreRank> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> list = getPredicates(root, query, cb, queryBean,accurate);
            Predicate[] preList = new Predicate[list.size()];
            return cb.and(list.toArray(preList));
        }

    }

    private static List<Predicate> getPredicates(Root<ScoreRank> root, CriteriaQuery<?> query, CriteriaBuilder cb,
            ScoreRank queryBean,boolean accurate) {
        // 条件集合
        List<Predicate> list = new ArrayList<>();

        // 年份条件
        String year = queryBean.getYear();
        // 省份条件
        String provinceName = queryBean.getProvinceName();
        // 学科方向条件
        Integer direction = queryBean.getDirection();
        // 分数条件
        int score = queryBean.getScore();
        if(accurate) {
            if (StringUtils.isNotBlank(year)) {
                list.add(cb.equal(root.get("year"), year.trim()));
            }
            if (StringUtils.isNotBlank(provinceName)) {
                list.add(cb.equal(root.get("provinceName"), provinceName.trim()));
            }
            if (direction != null && direction > 0) {
                list.add(cb.equal(root.get("direction"), direction));
            }
            if (score > 0) {
                list.add(cb.equal(root.get("score"), score));
            }
        } else {
            if (StringUtils.isNotBlank(year)) {
                list.add(cb.equal(root.get("year"), year.trim()));
            }
            if (StringUtils.isNotBlank(provinceName)) {
                list.add(cb.equal(root.get("provinceName"), provinceName.trim()));
            }
            if (direction != null && direction > 0) {
                list.add(cb.equal(root.get("direction"), direction));
            }
            if (score > 0) {
                list.add(cb.equal(root.get("score"), score));
            }
        }


        return list;
    }
}
